package com.news.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.news.entity.User;
import com.news.service.UserService;
import com.news.util.PageModel;

@Controller
@RequestMapping("/system/user")
public class UserController {

	@Resource(name = "userManager")
	private UserService userService;

	// tologin
	@RequestMapping(value = "/tologin")
	public String tologin(HttpSession httpSession) {
		httpSession.removeAttribute("USER");
		httpSession.removeAttribute("name");
		return "/login";

	}

	// tousertable
	@RequestMapping("/tousertable")
	public String tousertable() {
		return "/usertable";
	}

	// toadduser
	@RequestMapping("/toadd")
	public String toadduser() {
		return "/adduser";
	}

	// toadduser
	@RequestMapping("/toupdateuser")
	public String toupdateuser() {
		return "/updateuser";
	}

	// toindex2
	@RequestMapping("/toindex2")
	public String toindex2() {
		return "/index2";
	}

	@RequestMapping("/topicturetable")
	public String topicturetable() {
		return "/picturetable";
	}

	// login
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public User login(User user, HttpServletRequest request, HttpSession httpSession) {

		User u = userService.login(user);
		System.out.println("======================" + u.getUsername());
		System.out.println("======+++++++++++++++====" + u.getUsername());
		request.setAttribute("n", u);
		httpSession.setAttribute("USER", u);
		// 拦截通过条件
		httpSession.setAttribute("name", u.getName());
		return u;

	}

	// toadduser
	@RequestMapping("/toadduser")
	public String toAddUser() {
		return "/adduser2";
	}

	// adduser
	@RequestMapping(value = "/adduser", method = RequestMethod.POST)
	@ResponseBody
	public String adduser(User user, HttpServletRequest request) {

		List<User> u = userService.checkusername(user.getUsername());

		if (u != null && u.size() > 0) {

			return "1";
		} else {
			user.setIsdel(0);
			userService.addUser(user);
			return "0";
		}

	}

	// togetalluser
	@RequestMapping("/togetalluser")
	public String togetalluser() {

		return "/usertable";
	}

	// togetalluser
	@RequestMapping("/toiduser")
	public String togetall() {

		return "/iduser";
	}

	// getalluser
	@RequestMapping(value = "/getalluser")
	@ResponseBody
	public List<User> getalluser(User user, HttpServletRequest request) {
		List<User> u = userService.getAllUser();
		return u;
	}

	@RequestMapping("/get")
	@ResponseBody
	public PageModel get(HttpServletRequest request) {
		int page = 1;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		PageModel pageModel = userService.getPageModel(page, 4);
		// request.setAttribute("pageModel", pageModel);

		return pageModel;
	}

	// 模糊查询
	@RequestMapping("/checkname")
	@ResponseBody
	public PageModel checkname(String name, HttpServletRequest request) throws Exception {

		name = URLDecoder.decode(name, "UTF-8");

		int page = 1;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		PageModel pageModel = userService.getcheckalluser(name, page, 4);
		// request.setAttribute("pageModel", pageModel);

		return pageModel;
	}

	// id
	@RequestMapping("/getUser")
	public String getUser(int userid, HttpServletRequest request) {

		request.setAttribute("user", userService.getUser(userid));
		return "/updateuser";
	}

	// getbyid
	@RequestMapping("/getUserById")
	@ResponseBody
	public User getUserById(HttpServletRequest request, HttpSession httpSession) {
		User user = (User) httpSession.getAttribute("USER");
		int userid = user.getUserid();

		User u = userService.getUser(userid);
		request.setAttribute("user", userService.getUser(userid));
		return u;
	}

	// updateUser
	@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
	@ResponseBody
	public User updateUser(User user, HttpServletRequest request) {

		if (userService.updateUser(user)) {
			user = userService.getUser(user.getUserid());
			// request.setAttribute("user", user);
			return user;
		}
		return null;
	}

	@RequestMapping("/delUser")
	public void delUser(int userid, HttpServletResponse response) {

		String result = "{\"result\":\"error\"}";

		if (userService.delUser(userid)) {

			result = "{\"result\":\"success\"}";
		}

		response.setContentType("application/json");

		try {
			PrintWriter out = response.getWriter();
			out.write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 用户名唯一性
	@RequestMapping(value = "/updateUser")
	@ResponseBody
	public User checkusername(String username) {

		return null;
	}

}
